﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using System.Configuration;
using Jo.PropertyManagement.Common.Constants;
using Jo.PropertyManagement.Domain.Entity;

namespace Jo.PropertyManagement.DataAccess.Helper
{
    public class FluentHibernateHelper
    {
        private const string DB_CONNECTION_STRING = "DB_PRODUCT_MANAGEMENT";
        private static ISessionFactory _sessionFactory;

        public static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                    InitializeSessionFactory();

                return _sessionFactory;
            }
        }

        private static void InitializeSessionFactory()
        {
           MsSqlConfiguration msSqlConfg =  MsSqlConfiguration.MsSql2008
                               .ConnectionString(Constant.SQL_CONNECTION_STRING).ShowSql();
           FluentConfiguration flentConf = Fluently.Configure().Database(msSqlConfg);
            
            flentConf = flentConf.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Customer>());
            //flentConf = flentConf.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true));
            _sessionFactory = flentConf.BuildSessionFactory();
        }

        private static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }
    }
}
